package ch.zbw.swt.sysventory.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import ch.zbw.swt.sysventory.hibernate.entity.Scan;

public class SqlDataReader {

	private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	private static EntityTransaction tx = null;

	public SqlDataReader() {
		emf = Persistence
				.createEntityManagerFactory("ch.rheintalconnection.sysventory");
		em = emf.createEntityManager();

		tx = em.getTransaction();
	}

	public List<Scan> selectAll() {

		tx.begin();
		List<Scan> result = em.createQuery("from Scan", Scan.class)
				.getResultList();

		tx.commit();
		return result;
	}

	public Scan selectLast() {
		List<Scan> result = selectAll();
		Scan scan = result.get(result.size() - 1);

		return scan;
	}

}
